<style>
    .custom-selected{padding: 0em 0em 0em 0em;}
</style>
<div class="row-fluid clearfix">
    <div class="col-md-12 column">
        <ul class="breadcrumb">
            <li>
                <a href="index.html">首页</a>
            </li>
            <li class="active">
                操作员信息查询
            </li>
        </ul>
    </div>

    <div class="col-md-12 column">
        <div class="col-sm-12 div-page">
            <div class="col-sm-12 div-table">
                <div class="col-sm-12 custom-selected">
                    <div style="float: left">
                        <button type="button" class="btn btn-default btn-home">
                            <span class="glyphicon glyphicon-home"></span>&nbsp;<span id="orgName"></span>
                        </button>
                    </div>
                    <div class="col-sm-3">
                        <select class="form-control" id="level1">
                        </select>
                    </div>
                    <div class="col-sm-3" style="display: none">
                        <select class="form-control" id="level2">
                        </select>
                    </div>
                    <div class="col-sm-3" style="display: none">
                        <select class="form-control" id="level3">
                        </select>
                    </div>
                </div>
                <div id="custom-toolbar">

                </div>
                <table id="table" class="table table-hover table-condensed" data-click-to-select="true"  data-single-select="true" data-height="430" data-toolbar="#custom-toolbar">
                    <thead>
                    <tr>
                        <th data-field="state" data-checkbox="true"></th>
                        <th data-field="UserCode" data-halign="center" data-align="center">用户代码</th>
                        <th data-field="UserName" data-halign="center" data-align="center">用户名称</th>
                        <th data-field="OrganName" data-halign="center" data-align="center">所属机构</th>
                        <th data-field="Role_name" data-halign="center" data-align="center">用户类别</th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>

<script>

var orgid_new=$.cookie("OrgId");
var orgid_text=$.cookie("OrgName");
$("#orgName").text($.cookie("OrgName"));
$(function(){

    var btnHtml ;
    var btnRole = JSON.parse($.cookie("Role_note"));
console.log(btnRole);
    btnHtml = '<div class="form-inline" role="form">';
    btnHtml += (btnRole["CommonDataAdd"]) ? '<button id="btn-CommonDataAdd"  type="button" role="button" class="subFun btn btn-sm btn-primary">' +
            '<span class="glyphicon glyphicon-plus"></span> 新增</button> ' : '' ;
    btnHtml += (btnRole["CommonDataModify"]) ? '<button id="btn-CommonDataModify" type="button" role="button" class="subFun btn btn-sm btn-Data btn-success disabled">' +
            '<span class="glyphicon glyphicon-pencil"></span> 修改</button> ' : '' ;
    btnHtml += (btnRole["CommonDataCancel"]) ? '<button id="btn-CommonDataCancel"  type="button" role="button" class="subFun btn btn-sm btn-Data btn-danger disabled" >' +
            '<span class="glyphicon glyphicon-trash"></span> 删除</button> ' : '' ;
    btnHtml += (btnRole["CommonDataReset"]) ? '<button id="btn-ResetPassword"  type="button" role="button" class="btn btn-sm btn-Data btn-danger disabled" >' +
    '<span class="glyphicon glyphicon-trash"></span> 密码重置</button> ' : '' ;
    btnHtml += '<button id="btn-CommonDataDetail"  type="button" role="button" class="subFun btn btn-sm btn-Data btn-info disabled" >' +
            '<span class="glyphicon glyphicon-th-list"></span> 详情</button> ' ;

    $("#custom-toolbar").append(btnHtml);

    switch ($.cookie("OperClass")){
        case "1":
            FormatSelect(globalUrl+"api/Site?orgid=910",$("#level1"),"请选择","",function(resp){
                console.log(resp);
                $('#table').bootstrapTable({
                    url: globalUrl+"api/Oper?ParentId=910",
                    responseHandler:function(resp){
                        if(resp.data)
                        {
                            return resp.data.rows;
                        }
                        else
                        {
                            return null;
                        }
                    },
                    pagination:false,
                    search:true,
                    onClickRow:function(row, $element){
                        if ( $element.hasClass('selected') ) {
                            $element.removeClass('info');
                        }
                        else {
                            $element.parent('tbody').find(' > tr.selected ').removeClass("info");
                            $element.addClass('info');
                        }
                    },
                    onCheck:function(e,row,$element){
                        $(".btn-Data").removeClass("disabled");
                        //$("#btn-DataAdd").addClass("disabled");
                    },
                    onUncheck:function(){
                        $(".btn-Data").addClass("disabled");
                        //$("#btn-DataAdd").removeClass("disabled");
                    }
                });
            });
            break;
        case "2":
            $("#level1").parent().addClass("hidden");
            $("#level2").empty().parent().css("display","block");
            FormatSelect(globalUrl+"api/Site?orgid="+ $.cookie("OrgId"),$("#level2"),"请选择","",function(resp){});
            $('#table').bootstrapTable({
                url: globalUrl+"api/Oper?ParentId="+ $.cookie("OrgId"),
                responseHandler:function(resp){
                    if(resp.data)
                    {
                        return resp.data.rows;
                    }
                    else
                    {
                        return null;
                    }
                },
                pagination:false,
                search:true,
                onClickRow:function(row, $element){
                    if ( $element.hasClass('selected') ) {
                        $element.removeClass('info');
                    }
                    else {
                        $element.parent('tbody').find(' > tr.selected ').removeClass("info");
                        $element.addClass('info');
                    }
                },
                onCheck:function(e,row,$element){
                    $(".btn-Data").removeClass("disabled");
                    //$("#btn-DataAdd").addClass("disabled");
                },
                onUncheck:function(){
                    $(".btn-Data").addClass("disabled");
                    //$("#btn-DataAdd").removeClass("disabled");
                }
            });
            break;
        case "3":
            $("#level1").parent().addClass("hidden");
            $("#level3").empty().parent().css("display","block");
            FormatSelect(globalUrl+"api/Site?orgid="+ $.cookie("OrgId"),$("#level3"),"请选择","",function(resp){});
            $('#table').bootstrapTable({
                url: globalUrl+"api/Oper?ParentId="+ $.cookie("OrgId"),
                responseHandler:function(resp){
                    if(resp.data)
                    {
                        return resp.data.rows;
                    }
                    else
                    {
                        return null;
                    }
                },
                pagination:false,
                search:true,
                onClickRow:function(row, $element){
                    if ( $element.hasClass('selected') ) {
                        $element.removeClass('info');
                    }
                    else {
                        $element.parent('tbody').find(' > tr.selected ').removeClass("info");
                        $element.addClass('info');
                    }
                },
                onCheck:function(e,row,$element){
                    $(".btn-Data").removeClass("disabled");
                    //$("#btn-DataAdd").addClass("disabled");
                },
                onUncheck:function(){
                    $(".btn-Data").addClass("disabled");
                    //$("#btn-DataAdd").removeClass("disabled");
                }
            });
            break;
        case "4":
            $("#level1,#level2,#level3").parent().addClass("hidden");
            $(".custom-selected").addClass("hidden");
            $('#table').bootstrapTable({
                    url: globalUrl+"api/Oper?ParentId="+ $.cookie("OrgId"),
                    responseHandler:function(resp){
                        if(resp.data)
                        {
                            return resp.data.rows;
                        }
                        else
                        {
                            return null;
                        }
                    },
                    pagination:false,
                    search:true,
                    onClickRow:function(row, $element){
                        if ( $element.hasClass('selected') ) {
                            $element.removeClass('info');
                        }
                        else {
                            $element.parent('tbody').find(' > tr.selected ').removeClass("info");
                            $element.addClass('info');
                        }
                    },
                    onCheck:function(e,row,$element){
                        $(".btn-Data").removeClass("disabled");
                        //$("#btn-DataAdd").addClass("disabled");
                    },
                    onUncheck:function(){
                        $(".btn-Data").addClass("disabled");
                        //$("#btn-DataAdd").removeClass("disabled");
                    }
                });
            break;
    }
    $("#level1").change(function(){
        $("#level2").empty().parent().css("display","none");
        $("#level3").empty().parent().css("display","none");
        if($(this).val()!=""){
            var orgid=$(this).val();
            var orgtext=$(this).find("option:checked").text();
            FormatSelect(globalUrl+"api/Site?orgid="+$(this).val(),$("#level2"),"请选择","",function(resp){
                $.ajax({
                    type: "get",
                    url: globalUrl+"api/Oper?ParentId="+orgid,
                    dataType: "json",
                    success: function (resp) {
                        orgid_new=orgid;
                        orgid_text=orgtext;
                        try{
                            if(resp.data.rows)
                            {
                                $('#table').bootstrapTable('load',resp.data.rows );
                            }
                        }
                        catch(e) {
                            $('#table').bootstrapTable('load',[] );
                        }

                    },
                    error:function(){
                        alert("获取管理员信息失败！");
                    }
                });
                if($("#level2 option").length>0)
                {
                    $("#level2").parent().css("display","block");

                }
            });
        }


    });
    $("#level2").change(function(){
        $("#level3").empty().parent().css("display","none");
        if($(this).val()!=""){
            var orgid=$(this).val();
            var orgtext=$(this).find("option:checked").text();
            $.ajax({
                type: "get",
                url: globalUrl+"api/Oper?ParentId="+orgid,
                dataType: "json",
                success: function (resp) {
                    orgid_new=orgid;
                    orgid_text=orgtext;
                    try{
                        if(resp.data.rows)
                        {
                            $('#table').bootstrapTable('load',resp.data.rows );
                        }
                    }
                    catch(e) {
                        $('#table').bootstrapTable('load',[] );
                    }
                },
                error:function(){
                    alert("获取管理员信息失败！");
                }
            });
            FormatSelect(globalUrl+"api/Site?orgid="+$(this).val(),$("#level3"),"请选择","",function(resp){
                //level3=resp;
                if($("#level3 option").length>0)
                {
                    $("#level3").parent().css("display","block");
                }
            });
        }


    });
    $("#level3").change(function(){
        if($(this).val()!=""){
            var orgid=$(this).val();
            var orgtext=$(this).find("option:checked").text();
            $.ajax({
                type: "get",
                url: globalUrl+"api/Oper?ParentId="+orgid,
                dataType: "json",
                success: function (resp) {
                    orgid_new=orgid;
                    orgid_text=orgtext;
                    try{
                        if(resp.data.rows)
                        {
                            $('#table').bootstrapTable('load',resp.data.rows );

                        }
                    }
                    catch(e) {
                        $('#table').bootstrapTable('load',[] );
                    }
                },
                error:function(){
                    alert("获取管理员信息失败！");
                }
            });
        }

    });
    $(".btn-home").click(function(){
        switch ($.cookie("OperClass")){
            case "1":
                $("#level1").val("");
                orgid_text=$("#orgName").text();
                orgid_new=$.cookie("OrgId");
                $("#level2").parent().css("display","none");
                $("#level3").parent().css("display","none");
                break;
            case "2":
                $("#level2").val("");
                orgid_text=$("#orgName").text();
                orgid_new=$.cookie("OrgId");
                $("#level3").parent().css("display","none");
                break;
            case "3":
                $("#level3").val("");
                orgid_text=$("#orgName").text();
                orgid_new=$.cookie("OrgId");
                break;
        }
        $('#table').bootstrapTable('refresh',{url:globalUrl+"api/Oper?ParentId="+$.cookie("OrgId")});

    });

//子功能按钮
    $('.subFun ').on("click",function () {

        var fileName=$(this).attr("id").substr(14);
        var url="../assets/tpls/OperQuery_"+fileName+".html";

        getTpls(url,function(resp){
            $("#modal-Template").html(resp);
            //FormatSelect(globalUrl+"api/Dict?Typeid=0000000001",$("#Role_id"),"请选择","",function(data){});
            FormatSelect(globalUrl+"api/Role?org_id="+orgid_new,$("#Role_id"),"请选择","",function(data){
                switch (fileName){
                    case "Add":
                        $("#orgid_new").val(orgid_new);
                        $("#OrganName").val(orgid_text);
                        $('#validForm')
                                .formValidation({
                                    err: {
                                        container: 'popover'
                                    },
                                    icon: {
                                        valid: 'glyphicon glyphicon-ok',
                                        invalid: 'glyphicon glyphicon-remove',
                                        validating: 'glyphicon glyphicon-refresh'
                                    },
                                    addOns: {
                                        i18n: {}
                                    }
                                })
                                .formValidation('setLocale', 'zh_CN')
                                .on('success.form.fv', function(e) {
                                    e.preventDefault();
                                    $.ajax({
                                        type: "post",
                                        data:$('#validForm').serializeArray(),
                                        dataType: "json",
                                        url: globalUrl+"api/Oper_Info",
                                        success: function (resp) {
                                            if(resp.result)
                                            {
                                                $('#modal-Template').modal('hide');
                                                $('#table').bootstrapTable('refresh',{url:globalUrl+"api/Oper?ParentId="+orgid_new});
                                            }
                                            else
                                            {
                                                alert(resp.msg);
                                                //alert("后台返回数据为空！");
                                            }
                                        },
                                        error:function(){
                                            alert("后台未成功返回！");
                                        }
                                    });
                                });

                        break;
                    case "Modify":
                        //数据绑定
                        dataBindToModal($('#validForm'),$('#table'));
                        $("#orgid_new").val(orgid_new);
                        $('#validForm')
                                .formValidation({
                                    err: {
                                        container: 'popover'
                                    },
                                    icon: {
                                        valid: 'glyphicon glyphicon-ok',
                                        invalid: 'glyphicon glyphicon-remove',
                                        validating: 'glyphicon glyphicon-refresh'
                                    },
                                    addOns: {
                                        i18n: {}
                                    }
                                })
                                .formValidation('setLocale', 'zh_CN')
                                .on('success.form.fv', function(e) {
                                    e.preventDefault();
                                    $.ajax({
                                        type: "put",
                                        data:$('#validForm').serializeArray(),
                                        dataType: "json",
                                        url: globalUrl+"api/Oper_Info",
                                        success: function (resp) {
                                            if(resp.result)
                                            {
                                                //console.log(resp);
                                                $('#modal-Template').modal('hide');
                                                $('#table').bootstrapTable('refresh',{url:globalUrl+"api/Oper?ParentId="+orgid_new});
                                            }
                                            else
                                            {
                                                alert(resp.msg);
                                                //alert("后台返回数据为空！");
                                            }
                                        },
                                        error:function(){
                                            alert("后台未成功返回！");
                                        }
                                    });
                                });


                        break;
                    case "Cancel":
                        //数据绑定
                        dataBindToModal($('#validForm'),$('#table'));
                        var PId=$("#PId").val();
                        var PadId=$("#PadId").val();
                        var TermId=$("#TermId").val();


                        $('#modal-btn-Del').click(function (e) {
                            e.preventDefault();
                            $.ajax({
                                type: "delete",
                                dataType: "json",
                                url: globalUrl+"api/Oper_Info?UserCode="+$("#UserCode").val(),
                                success: function (resp) {
                                    if(resp.result)
                                    {
                                        $('#modal-Template').modal('hide');
                                        $('#table').bootstrapTable('refresh',{url:globalUrl+"api/Oper?ParentId="+orgid_new});
                                    }
                                    else
                                    {
                                        alert(resp.msg);
                                        //alert("后台返回数据为空！");
                                    }
                                },
                                error:function(){
                                    alert("后台未成功返回！");
                                }
                            });
                        });

                        break;
                    default:
                        //数据绑定
                        dataBindToModal($('#validForm'),$('#table'));
                        var PId=$("#PId").val();
                        var PadId=$("#PadId").val();
                        var TermId=$("#TermId").val();
                }
                $('#modal-Template').modal('show');
            });

        });
    });

    ///密码重置
    $("#btn-ResetPassword").click(function(){
        if($('#table').bootstrapTable('getSelections')[0]!=undefined){
            var UserCode = $('#table').bootstrapTable('getSelections')[0]["UserCode"];
            if(UserCode== $.cookie("Custom-Auth-Name"))
            {
                alert("当前用户不允许重置密码！");
                return;
            }
            var flag= confirm("确认重置密码吗？");
            if(flag)
            {
                $.ajax({
                    type: "put",
                    dataType: "json",
                    url: globalUrl+"api/Oper_Info?UserCode="+UserCode,
                    success: function (resp) {
                        if(resp.result)
                        {
                            alert(resp.msg);
                        }
                        else
                        {
                            alert(resp.msg);
                            //alert("后台返回数据为空！");
                        }
                    },
                    error:function(){
                        alert("后台未成功返回！");
                    }
                });
            }


        }


    });
});
</script>